<?php

namespace App\Http\Controllers\Admin;

use App\Models\Author;
use App\Models\Book;
use Illuminate\Http\Request;

class AuthorController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('check_admin_login');
    }

    public function index(Request $request)
    {
        $page                         = intval($request->get('page',1));
        $page                         = max($page ,1);
        $page_size                    = intval($request->get('page_size',10));

        $builder = Author::query()->where('deleted',0);
        $columns = ['id','author_name','country','author_image','intro','list_image','sort'];
        $list = $builder->orderBy('sort','desc')->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);

        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'           => $item->id,
                    'author_name'  => $item->author_name,
                    'country'      => $item->country,
                    'author_image' => $item->author_image,
                    'list_image'   => $item->list_image,
                    'intro'        => $item->intro,
                    'sort'          => $item->sort,
                    'book_num'     => Book::query()->where('author',$item->author_name)->where('deleted',0)->count(),
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function store(Request $request)
    {
        $author_name  = trim($request->get('author_name'));
        $country      = trim($request->get('country'));
        $author_image = trim($request->get('author_image'));
        $intro        = trim($request->get('intro'));
        $list_image   = trim($request->get('list_image'));
        $sort         = intval($request->get('sort'));

        if(empty($author_name) || empty($author_image) || empty($country) || empty($intro) || empty($list_image))
        {
            return $this->err(1,200,'','参数错误');
        }

        $row = Author::query()->where('author_name',$author_name)->where('deleted',0)->first();
        if(!empty($row))
        {
            return $this->err(1,200,'','该名家已存在');
        }


        $data = [
            'author_name'  => $author_name,
            'country'      => $country,
            'author_image' => $author_image,
            'intro'        => $intro,
            'list_image'   => $list_image,
            'sort'         => $sort,
            'created_at'   => $this->time,
            'updated_at'   => $this->time,
        ];
        $author = Author::create($data);
        return $this->ok([],'添加成功');
    }

    public function update(Request $request)
    {
        $id           = intval($request->get('id'));
        $author_name  = trim($request->get('author_name'));
        $country      = trim($request->get('country'));
        $author_image = trim($request->get('author_image'));
        $intro        = trim($request->get('intro'));
        $list_image   = trim($request->get('list_image'));
        $sort         = intval($request->get('sort'));

        if(empty($id) || empty($author_name) || empty($author_image) || empty($country) || empty($intro) || empty($list_image))
        {
            return $this->err(1,200,'','参数错误');
        }

        $row = Author::query()->where('author_name',$author_name)->where('deleted',0)->first();
        if(!empty($row) && $row->id != $id)
        {
            return $this->err(1,200,'','该名家已存在');
        }

        $author = $this->_check_info($id);
        if(empty($author))
        {
            return $this->err(1,200,'','名家不存在');
        }

        $data = [
            'author_name'  => $author_name,
            'country'      => $country,
            'author_image' => $author_image,
            'intro'        => $intro,
            'list_image'   => $list_image,
            'updated_at'   => $this->time,
            'sort'         => $sort,
        ];
        $author->update($data);
        return $this->ok([],'修改成功');
    }

    public function show(Request $request)
    {
        $id = intval($request->get('id'));
        $author = $this->_check_info($id);
        if(empty($author))
        {
            return $this->err(1,200,'','名家不存在');
        }



        //获取图书列表
        $list = Book::query()->select(['id','image','title','total_stock','new_book_price','new_book_stock','good_book_price','good_book_stock','medium_book_price','medium_book_stock'])->where('deleted',0)->where('author',$author->author_name)->orderBy('total_stock','desc')->get();

        $items = [];
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $items[] = [
                    'id'                => $v->id,
                    'image'             => $v->image,
                    'title'             => $v->title,
                    'total_stock'       => $v->total_stock,
                    'new_book_price'    => $v->new_book_price,
                    'good_book_price'   => $v->good_book_price,
                    'medium_book_price' => $v->medium_book_price,
                ];
            }
        }


        $result = [
            'id' => $id,
            'author_name'  => $author->author_name,
            'country'      => $author->country,
            'author_image' => $author->author_image,
            'intro'        => $author->intro,
            'list_image'   => $author->list_image,
            'items'        => $items
        ];

        return $this->ok($result);
    }

    private function _check_info($id)
    {
        $author = Author::query()->where('id',$id)->where('deleted',0)->first();
        return $author;
    }

    public function destroy(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $author = $this->_check_info($id);
        if(empty($author))
        {
            return $this->err(1,200,'','名家不存在');
        }

        if(!$author->deleted)
        {
            $author->update([
                'deleted'      => 1,
                'deleted_at'   => $this->time
            ]);
            return $this->ok([],'删除成功');
        }

        return $this->err(1,200,'','删除失败');
    }
}
